package com.szsh.bigdata.open.utils

import org.redisson.Redisson
import org.redisson.api.{RBucket, RedissonClient}
import org.redisson.client.codec.StringCodec
import org.redisson.config.Config

import java.util.concurrent.TimeUnit

object RedissonUtil extends Serializable {
	
	private val REDIS_CLUSTER: String = "xxx.xxx.xxx.xxx:port,xxx.xxx.xxx.xxx:port"
	private val REDIS_PASSWORD: String = "xxx"
	
	def init(): RedissonClient = {
		val config: Config = new Config()
		config.setCodec(new StringCodec())
		config.useClusterServers()
			.setScanInterval(200)
			.addNodeAddress(REDIS_CLUSTER.split(""","""):_*).setPassword(REDIS_PASSWORD)
		val redisson: RedissonClient = Redisson.create(config)
		redisson
	}
	
	def setString(redissonClient: RedissonClient, k: String, v: String, time: Int = 0): Unit = {
		println(s"k: $k , v: $v")
		val rBucket: RBucket[String] = redissonClient.getBucket(k)
		if (time == 0) {
			rBucket.set(v)
		}else {
			rBucket.set(v, time, TimeUnit.SECONDS)
		}
	}
	
	def getString(redissonClient: RedissonClient, k: String): String = {
		val rBucket: RBucket[String] = redissonClient.getBucket(k)
		rBucket.get()
	}
	
}
